package com.xl.web.controller.sys;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xl.base.annotation.Auth;
import com.xl.base.annotation.Duplicate;
import com.xl.base.web.BaseController;
import com.xl.core.enums.CommonEnum;
import com.xl.core.util.StringUtil;
import com.xl.web.entity.sys.SysMenu;
import com.xl.web.service.sys.SysMenuService;

/**
 * 系统后台菜单管理控制器
 * 
 * @author 梁华
 * 
 */
@Controller
@Scope("prototype")
@RequestMapping("/sys/menu")
public class SysMenuController extends BaseController {

	// 注入系统菜单管理service
	@Autowired(required = false)
	private SysMenuService<SysMenu> sysMenuService;

	/**
	 * 菜单一级列表
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.menu.list")
	@RequestMapping("/list")
	public ModelAndView list(SysMenu menu) throws Exception {

		Map<String, Object> context = getRootMap(request);
		return forword("sys/menu_list", context);
	}

	/**
	 * 菜单一级列表数据
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.menu.list")
	@ResponseBody
	@RequestMapping("/listData")
	public void listData() throws Exception {

		SysMenu menu = (SysMenu)getJsonParams(SysMenu.class);
		menu.getPager().setOrderField("create_date");
		menu.setMenuGrade(CommonEnum.menu_grade.GRADE_1.getValue());
		// 查询列表
		List<SysMenu> dataList = sysMenuService.queryByList(menu);
		sendListData(response, menu.getPager().getRowCount(), dataList);
	}
	
	/**
	 * 菜单二级列表
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.menu.list")
	@RequestMapping("/childrenMenu")
	public ModelAndView childrenMenu(SysMenu menu) throws Exception {

		Map<String, Object> context = getRootMap(request);
		context.put("entity", menu);
		return forword("sys/menu_child_list", context);
	}
	
	/**
	 * 菜单二级列表数据
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.menu.list")
	@ResponseBody
	@RequestMapping("/listChildrenData")
	public void listChildrenData() throws Exception {

		SysMenu menu = (SysMenu)getJsonParams(SysMenu.class);
		menu.getPager().setOrderField("create_date");
		menu.setMenuGrade(CommonEnum.menu_grade.GRADE_2.getValue());
		// 查询列表
		List<SysMenu> dataList = sysMenuService.queryByList(menu);
		sendListData(response, menu.getPager().getRowCount(), dataList);
	}

	/**
	 * 新增菜单页面
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.menu.handle")
	@Duplicate(add = true)
	@RequestMapping("/add")
	public ModelAndView add() throws Exception {

		Map<String, Object> context = getRootMap(request);

		// 查询一级菜单列表
		SysMenu menu = new SysMenu();
		menu.getPager().setOrderField("create_date");
		menu.setMenuGrade(CommonEnum.menu_grade.GRADE_1.getValue());
		menu.getPager().setLimitDirection(false);
		List<SysMenu> dataList = sysMenuService.queryByList(menu);
		context.put("dataList", dataList);

		return forword("sys/menu_add", context);
	}

	/**
	 * 修改菜单页面
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.menu.handle")
	@Duplicate(add = true)
	@RequestMapping("/update")
	public ModelAndView update(SysMenu menu) throws Exception {

		Map<String, Object> context = getRootMap(request);

		// 查询本菜单的详细数据
		menu = sysMenuService.queryById(menu);
		context.put("menu", menu);

		// 查询一级菜单列表
		SysMenu menuCk = new SysMenu();
		menu.getPager().setOrderField("create_date");
		menuCk.setMenuGrade(CommonEnum.menu_grade.GRADE_1.getValue());
		menuCk.getPager().setLimitDirection(false);
		List<SysMenu> dataList = sysMenuService.queryByList(menuCk);
		context.put("dataList", dataList);

		return forword("sys/menu_update", context);
	}

	/**
	 * 新增菜单保存
	 * 
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.menu.handle")
	@Duplicate(validate = true)
	@ResponseBody
	@RequestMapping("addSave")
	public void addSave(SysMenu menu) throws Exception {

		// 新增菜单保存
		if (StringUtil.isEmpty(menu.getId())) {
			sysMenuService.add(menu);
		} else {
			// 更新菜单保存
			sysMenuService.updateBySelective(menu);
		}

		sendSuccessMessage(response, "添加成功");
	}

	/**
	 * 删除菜单保存
	 * 
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.menu.handle")
	@ResponseBody
	@RequestMapping("deleteMenu")
	public void deleteMenu(SysMenu menu) throws Exception {

		// 检测是否存在子菜单
		SysMenu checkMenu = sysMenuService.queryById(menu);
		if (checkMenu != null && checkMenu.getChildMenuList() != null
				&& checkMenu.getChildMenuList().size() > 0) {
			sendFailureMessage(response, "请先删除子菜单");
			return;
		}
		// 逻辑删除本条数据
		sysMenuService.delete(menu.getId());

		sendSuccessMessage(response, "删除成功");
	}
}
